package GUI;

import java.util.ArrayList;

import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JButton;

import Client.BusinessImplements;
import Controler.Update_User_Controler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
 * @author Lucie
 * Class name Update_User_secon_Panel
 * extend  Abstract_Jpanel
 * purpose: update the details of the User that the user choose
 */
public class Update_User_secon_Panel extends Abstract_Jpanel  {

	private ArrayList<String> user;
	private JTextField textFieldid;
	private JTextField textField_email;
	private JTextField textField_pass;
	private JComboBox comboBoxrole;
	/**
	 * Create the panel.
	 */
	
	public Update_User_secon_Panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(65, 61, 730, 328);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(159, 50, 454, 231);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblUserId = new JLabel("user id:");
		lblUserId.setBounds(26, 11, 61, 26);
		panel_1.add(lblUserId);
		lblUserId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldid = new JTextField();
		textFieldid.setBounds(131, 16, 109, 20);
		panel_1.add(textFieldid);
		textFieldid.setEnabled(false);
		textFieldid.setColumns(10);
		
		JLabel lblEmail = new JLabel("email:");
		lblEmail.setBounds(26, 58, 46, 14);
		panel_1.add(lblEmail);
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textField_email = new JTextField();
		textField_email.setBounds(131, 57, 109, 20);
		panel_1.add(textField_email);
		textField_email.setColumns(10);
		
		JLabel lblUserPassworld = new JLabel("passworld:");
		lblUserPassworld.setBounds(26, 100, 109, 26);
		panel_1.add(lblUserPassworld);
		lblUserPassworld.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textField_pass = new JTextField();
		textField_pass.setBounds(131, 105, 109, 20);
		panel_1.add(textField_pass);
		textField_pass.setColumns(10);
		
		JLabel lblUserRole = new JLabel("role:");
		lblUserRole.setBounds(26, 145, 72, 26);
		panel_1.add(lblUserRole);
		lblUserRole.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		 comboBoxrole = new JComboBox();
		 comboBoxrole.setBounds(131, 150, 109, 20);
		 panel_1.add(comboBoxrole);
		 comboBoxrole.setModel(new DefaultComboBoxModel(new String[] {"", "librarian", "maneger"}));
		
		JLabel lblUpdateUser = new JLabel("Update User");
		lblUpdateUser.setForeground(new Color(25, 25, 112));
		lblUpdateUser.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblUpdateUser.setBounds(65, 11, 222, 34);
		add(lblUpdateUser);
		//btnBack=goes back to Update_User_first_Panel
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Update_User_first_Panel next_screen=new Update_User_first_Panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(next_screen);
				next_screen.setSize(getMaximumSize());
				next_screen.on_screen_load();
				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(76, 420, 89, 35);
		add(btnBack);
		/*
		 * btnUpdate; call the controller of this panel and update the details of the user
		 * if something goes wrong display a message
		 */
		JButton btnUpdate = new JButton("update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Update_User_Controler update_user_controler=new Update_User_Controler();
		
				if(update_user_controler.update_User(textFieldid.getText(),textField_email.getText(),textField_pass.getText(),get_role()))
				{
					next_screen_success();
					ArrayList<String> str_check=new  ArrayList<String>();
					str_check=update_user_controler.get_user(user.get(0));
					if(str_check!=null)
					{
					on_screen_load(str_check);
					}
					
					
				}
				else {
					next_screen_faild(update_user_controler.get_msg());
					
				}
				
				
				
			}
		});
		btnUpdate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnUpdate.setBounds(697, 420, 94, 34);
		add(btnUpdate);
		

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}
	/**
	   * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox and text fields
	 * that this panel use, 
	 * @param str_user
	 */
	public void on_screen_load(ArrayList<String> str_user) {
		user=new ArrayList<String>();
		user=str_user;

		
		 textFieldid.setText(user.get(0));
		textField_email.setText(user.get(1));
		textField_pass.setText(user.get(2));
		if (user.get(3).equals("2"))
		comboBoxrole.setSelectedIndex(1);
		else
		comboBoxrole.setSelectedIndex(2);
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 *  this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 *  this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	
	
	/**
	 * if role ="librarian" return "2"
	 * if role ="maneger" return "3"
	 * @return
	 */
	public String get_role()
	{
		if(comboBoxrole.getSelectedItem().toString().equals("librarian"))
			return "2";
		if(comboBoxrole.getSelectedItem().toString().equals("maneger"))
		return"3";
		return "";
	}

}
